let studentsArr = [];

module.exports = (app) => {
    app.use((req, res, next) => {
        let str = '';
        req.on("data", (chunk) => {
            str += chunk;
        })
        req.on("end", () => {
            req.body = str;
            next();
        })
    })
    app.get("/text", (req, res, next) => {
        res.send(JSON.stringify({ code: 1, data: studentsArr }))
    })
    app.post("/add", (req, res, next) => {
        studentsArr.push(JSON.parse(req.body))
        res.send(JSON.stringify({ code: 1, msg: "添加成功!" }))
    })
    app.post("/del", (req, res, next) => {
        studentsArr.splice(JSON.parse(req.body).index, 1)
        res.send(JSON.stringify({ code: 1, msg: "删除成功!" }))
    })
    app.post("/change", (req, res, next) => {
        req.body = JSON.parse(req.body);
        studentsArr[req.body.index] = { name: req.body.name, score: req.body.score }
        res.send(JSON.stringify({ code: 1, msg: "修改成功!", data: studentsArr }))
    })
}